package kr.couponplay.android.activity.search;

import java.util.ArrayList;
import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.domain.BeanFactory;
import kr.couponplay.android.domain.query.QueryDao;
import kr.couponplay.android.domain.query.RecentQuery;
import kr.couponplay.android.domain.query.RecentQueryList;
import kr.couponplay.android.support.AlertUtils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@SuppressLint("HandlerLeak") 
public class RecentQueryFragment extends Fragment implements OnItemClickListener, PositionAware {
	private SearchActivity activity;
	private QueryListAdapter queryListAdapter;
	private ListView queryListView;
	
	private List<RecentQuery> recentQuries;
	
	public static RecentQueryFragment newInstance(SearchActivity activity) {
		RecentQueryFragment fragment = new RecentQueryFragment();
		fragment.activity = activity;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View onCreateView = inflater.inflate(R.layout.search_query_fragment, container, false);
		queryListAdapter = new QueryListAdapter(container.getContext(), R.layout.search_query_list_row, new ArrayList<RecentQuery>());

		queryListView = (ListView) onCreateView.findViewById(R.id.recent_query_list);
        queryListView.setAdapter(queryListAdapter);
        queryListView.setOnItemClickListener(this);
        getDataFromServer(container.getContext());
		return onCreateView;
	}
	
    private void getDataFromServer(final Context context) {
        Thread thread = new Thread(new Runnable() {
            public void run() {
            	Message msg = Message.obtain();
            	try {
            		msg.obj = BeanFactory.getBean(context.getApplicationContext(), QueryDao.class).selectRecentQueries(AppContext.getSid(context.getApplicationContext()));
            	} catch (Exception e) {
            		msg.obj = e;
            	}
        		handler.sendMessage(msg);
            }
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
	    	if (msg.obj instanceof Throwable) {
	    		alert((Exception)msg.obj);
	    		return;
	    	}
        	
	    	recentQuries = ((RecentQueryList)msg.obj).getRecentQueries();
            drawDealListView();
        }
    };
    
    public void alert(Exception exception) {
        AlertUtils.errorAlert(activity, exception);
    }
    
	private void drawDealListView() {
		for (RecentQuery each : recentQuries) {
			queryListAdapter.add(each);
		}
		queryListAdapter.notifyDataSetChanged();
	}    
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		activity.searchInput.setText(queryListAdapter.getItem(position).getQuery());
		activity.goSearch();
		AppContext.isClearSearchDeals = true;
	}	

	@Override
	public int getPosition() {
		return 0;
	}
}
